using System;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace CascadingNav
{
    [Guid("7280966d-c864-42c3-9248-6409d6778f69")]
    public class CascadingNav : System.Web.UI.WebControls.WebParts.WebPart
    {
        public string _AdminList = "";

        public CascadingNav()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        [Personalizable, WebBrowsable, WebDisplayName("Admin List")]
        public string Set_AdminList
        {
            get
            {
                return _AdminList;
            }
            set
            {
                _AdminList = value;
            }
        }

        protected override void CreateChildControls()
        {
            // Create ASPMenu control.
            AspMenu _menu;
            string MenuID = _AdminList + "CustomMenu";
            _menu = new AspMenu();

            //Set the controls look and feel
            _menu.ID = "CustomMenu";
            _menu.EnableViewState = false;
            _menu.Orientation = System.Web.UI.WebControls.Orientation.Horizontal;
            _menu.StaticDisplayLevels = 1;
            _menu.MaximumDynamicDisplayLevels = 3;
            _menu.StaticSubMenuIndent = 0;
            _menu.DynamicHorizontalOffset = 0;
            _menu.StaticPopOutImageUrl = "/_layouts/images/menudark.gif";
            _menu.StaticPopOutImageTextFormatString = "";
            _menu.DynamicHoverStyle.BackColor = Color.FromName("#CBE3F0");
            _menu.SkipLinkText = "";
            _menu.StaticSubMenuIndent = 0;
            _menu.CssClass = "ms-topNavContainer";

            _menu.StaticMenuItemStyle.CssClass = "ms-topnav";
            _menu.StaticMenuItemStyle.ItemSpacing = 0;
            _menu.StaticSelectedStyle.CssClass = "ms-topnavselected";
            _menu.StaticHoverStyle.CssClass = "ms-topNavHover";
            _menu.DynamicMenuStyle.BackColor = Color.FromName("#F2F3F4");
            _menu.DynamicMenuStyle.BorderColor = Color.FromName("#A7B4CE");
            _menu.DynamicMenuStyle.BorderWidth  = 1;
            _menu.DynamicHoverStyle.BackColor = Color.FromName("#CBE3F0");
            _menu.DynamicHoverStyle.BorderColor = Color.FromName("#CBE3F0");
            _menu.DynamicMenuItemStyle.CssClass = "ms-topNavFlyOuts";
            _menu.DynamicHoverStyle.CssClass = "ms-topNavFlyOutsHover";
            _menu.DynamicSelectedStyle.CssClass = "ms-topNavFlyOutsSelected";

            SPWeb thisWeb = null;

            try
            {

                SPSite thisSite = SPControl.GetContextSite(Context);
                thisWeb = thisSite.OpenWeb("/");
                SPList spCascadingNav = thisWeb.Lists[_AdminList];

                SPQuery query = new SPQuery();
                //query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 1</Value></Eq><Eq><FieldRef Name='Display' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 1</Value></Eq><Eq><FieldRef Name='Display0' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                SPListItemCollection items = spCascadingNav.GetItems(query);

                //For each item, generate the row.
                System.Web.UI.WebControls.MenuItem _item = new System.Web.UI.WebControls.MenuItem();
                System.Web.UI.WebControls.MenuItem _item2;
                System.Web.UI.WebControls.MenuItem _item3;

                foreach (SPListItem menuItem in items)
                {
                    string parentID = " ";
                    if (menuItem["Parent ID"] != null)
                    {
                        parentID = menuItem["Parent ID"].ToString();
                    }
                    else { parentID = "None"; }
                    string title = menuItem["Title"].ToString();
                    string url = menuItem["Link URL"].ToString();
                    string itemID = menuItem["Link ID"].ToString();

                    _item = new System.Web.UI.WebControls.MenuItem(title, "", "", url);

                    SPQuery level2query = new SPQuery();
                    level2query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 2</Value></Eq><Eq><FieldRef Name='Display0' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                    //level2query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 2</Value></Eq><Eq><FieldRef Name='Display' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                    SPListItemCollection level2items = spCascadingNav.GetItems(level2query);

                    foreach (SPListItem subMenuItem in level2items)
                    {
                        string subparentID = " ";
                        if (subMenuItem["Parent ID"] != null)
                        {
                            subparentID = subMenuItem["Parent ID"].ToString();
                        }
                        else { subparentID = "None"; }

                        if (subparentID == itemID)
                        {
                            string subtitle = subMenuItem["Title"].ToString();
                            string suburl = subMenuItem["Link URL"].ToString();
                            string subID = subMenuItem["Link ID"].ToString();

                            _item2 = new System.Web.UI.WebControls.MenuItem(subtitle, "", "", suburl);

                            SPQuery level3query = new SPQuery();
                            //level3query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 3</Value></Eq><Eq><FieldRef Name='Display' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                            level3query.Query = "<OrderBy><FieldRef Name='Link_x0020_Order' /></OrderBy><Where><And><Eq><FieldRef Name='Item_x0020_Level' /><Value Type='Choice'>Level 3</Value></Eq><Eq><FieldRef Name='Display0' /><Value Type='Choice'>Yes</Value></Eq></And></Where>";
                            SPListItemCollection level3items = spCascadingNav.GetItems(level3query);

                            foreach (SPListItem sub3MenuItem in level3items)
                            {
                                string sub3parentID = " ";
                                if (sub3MenuItem["Parent ID"] != null)
                                {
                                    sub3parentID = sub3MenuItem["Parent ID"].ToString();
                                }
                                else { sub3parentID = "None"; }
                                if (sub3parentID == subID)
                                {
                                    string sub3title = sub3MenuItem["Title"].ToString();
                                    string sub3url = sub3MenuItem["Link URL"].ToString();

                                    _item3 = new System.Web.UI.WebControls.MenuItem(sub3title, "", "", sub3url);
                                    _item2.ChildItems.Add(_item3);
                                }
                            }
                            _item.ChildItems.Add(_item2);
                        }

                    }
                    _menu.Items.Add(_item);

                    Controls.Add(_menu);
                }
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl("An error has occured with this web part.  Please contact your system administrator and relay this error message: " + ex.Message));
            }
            finally
            {
                if (thisWeb != null)
                thisWeb.Dispose();
            }
            
        }
       
    }
}
